/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.william;

import static l1j.server.server.model.skill.L1SkillId.CURSE_PARALYZE;
import static l1j.server.server.model.skill.L1SkillId.DECAY_POTION;
import static l1j.server.server.model.skill.L1SkillId.ABSOLUTE_BARRIER;
import static l1j.server.server.model.skill.L1SkillId.FOG_OF_SLEEPING;
import static l1j.server.server.model.skill.L1SkillId.FREEZING_BLIZZARD;
import static l1j.server.server.model.skill.L1SkillId.FREEZING_BREATH;
import static l1j.server.server.model.skill.L1SkillId.ICE_LANCE;
import static l1j.server.server.model.skill.L1SkillId.ICE_LANCE_BASILISK;
import static l1j.server.server.model.skill.L1SkillId.ICE_LANCE_COCKATRICE;
import static l1j.server.server.model.skill.L1SkillId.SHOCK_STUN;
import static l1j.server.server.model.skill.L1SkillId.EARTH_BIND;
import static l1j.server.server.model.skill.L1SkillId.BONE_BREAK;
import static l1j.server.server.model.skill.L1SkillId.POLLUTE_WATER;
import static l1j.server.server.model.skill.L1SkillId.STATUS_CURSE_PARALYZED;

import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_ItemName;
import l1j.server.server.serverpackets.S_SkillSound;
import l1j.server.server.serverpackets.S_SystemMessage;
import java.util.Random;

/**
 *	自动补血道具
 */
public class SkillSound_autohp extends TimerTask {

	private static Logger _log = Logger.getLogger(SkillSound.class.getName());

	private static final Random _random = new Random();

	private final L1PcInstance _pc;

	private final float _addBlood;

	public SkillSound_autohp(L1PcInstance pc,float paramFloat) { // 建构
		_pc = pc;
		_addBlood = paramFloat;
	}

	@Override
	public void run() {
		try {
			if (_pc.isDead()) {
				return;
			}

			// 以下状态不执行
			if (_pc.isParalyzed()
					|| _pc.hasSkillEffect(CURSE_PARALYZE)		// 木乃伊的诅咒
					|| _pc.hasSkillEffect(STATUS_CURSE_PARALYZED) // 木乃尹状态
					|| _pc.hasSkillEffect(DECAY_POTION)			// 药水霜化术
					|| _pc.hasSkillEffect(ABSOLUTE_BARRIER) 	// 绝对屏障
					|| _pc.hasSkillEffect(SHOCK_STUN)			// 冲击之晕
					|| _pc.hasSkillEffect(EARTH_BIND)			// 大地屏障
					|| _pc.hasSkillEffect(FOG_OF_SLEEPING)		// 沉睡之雾
					|| _pc.hasSkillEffect(ICE_LANCE)			// 冰矛围篱
					|| _pc.hasSkillEffect(FREEZING_BLIZZARD) 	// 冰雪飓风
					|| _pc.hasSkillEffect(FREEZING_BREATH)		// 寒冰喷吐
					|| _pc.hasSkillEffect(ICE_LANCE_COCKATRICE)	// 亚力安冰矛围篱
					|| _pc.hasSkillEffect(ICE_LANCE_BASILISK)	// 邪恶蜥蜴冰矛围篱
					|| _pc.hasSkillEffect(BONE_BREAK)) {		// 骷髅毁坏
				return;
			}

			// 自动补血
			int pcLV = _pc.getLevel();		// 玩家等级
			int pcHP = _pc.getCurrentHp();	// 玩家目前 HP
			int autoHP = (short)(_pc.getMaxHp() * _addBlood);
			int auto_HP = 55;
			auto_HP = (int)(auto_HP * (_random.nextGaussian() / 5.0D + 1.0D));
			if (this._pc.hasSkillEffect(POLLUTE_WATER)) { // 污浊之水效果减半
				auto_HP /= 2;
			}

			if (_pc.getInventory().checkItem(40024)) { // 检查身上道具
				if (_pc.getInventory().checkItem(5086) && (pcHP < autoHP)) {
					_pc.setCurrentHp(pcHP+(auto_HP + pcLV));
					_pc.sendPackets(new S_SkillSound(_pc.getId(), 197)); // 魔法图示
					_pc.broadcastPacket(new S_SkillSound(_pc.getId(), 197));
					if (auto_HP > 150) { // 补血量 高于150
						_pc.getInventory().consumeItem(40024, 2); // 扣除2瓶药水
					} else {
						_pc.getInventory().consumeItem(40024, 1); // 扣除1瓶药水
					}
				}
			} else {
				_pc.stopSkillSound_autoHP(); // 自动停止
				_pc.sendPackets(new S_SystemMessage("无古代终极体力恢复剂 自动补血关闭。"));
				AutoHpClose(_pc);
			}

			// 无法使用道具的地图
			if (!_pc.getMap().isUsableItem()) {
				_pc.stopSkillSound_autoHP(); // 自动停止
				_pc.sendPackets(new S_SystemMessage("这个地图无法使用自动补血，关闭自动补血。"));
				AutoHpClose(_pc);
				return;
			}
		} catch (Throwable e) {
			_log.log(Level.WARNING, e.getLocalizedMessage(), e);
		}
	}

	private void AutoHpClose(L1PcInstance pc) {
		L1ItemInstance ItemInstance1 = pc.getInventory().findItemId(5083); // 自动补血道具40%
	    L1ItemInstance ItemInstance2 = pc.getInventory().findItemId(5084); // 自动补血道具75%
	    L1ItemInstance ItemInstance3 = pc.getInventory().findItemId(5085); // 自动补血道具85%
	    L1ItemInstance ItemInstance4 = pc.getInventory().findItemId(5086); // 自动补血道具95%
	    if (ItemInstance1 != null) {
	    	ItemInstance1.setNowAutoHp(false);
	    	pc.sendPackets(new S_ItemName(ItemInstance1));
	    }
	    if (ItemInstance2 != null) {
	    	ItemInstance2.setNowAutoHp(false);
	    	pc.sendPackets(new S_ItemName(ItemInstance2));
	    }
	    if (ItemInstance3 != null) {
	    	ItemInstance3.setNowAutoHp(false);
	    	pc.sendPackets(new S_ItemName(ItemInstance3));
	    }
	    if (ItemInstance4 != null) {
	    	ItemInstance4.setNowAutoHp(false);
	    	pc.sendPackets(new S_ItemName(ItemInstance4));
	    }
	    ItemInstance1 = null;
	    ItemInstance2 = null;
	    ItemInstance3 = null;
	    ItemInstance4 = null;
	    pc = null;
	}
}
